package com.example.demo.test.alc.login;

import com.example.demo.socket.WebSocketServer;
import com.example.demo.test.selenium.SeleniumHelper;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
public class AccountLoginBySSO extends SeleniumHelper implements AccountLoginStrategy {
    @Override
    public void login() {
        StopWatch watch = new StopWatch("account-sso");
        WebSocketServer.screenWarnMsg("开始: loginBySSO");
        Boolean my_profile = null;
        String loginUrl = "https://account.lenovo.com/us/en/";
        String lsession = "E510AB1E-10D6-42E9-81FD-3C115B57811D";
        try {

            watch.start();
            WebSocketServer.screenInfoMsg("清除浏览器cookie");
            driver.manage().deleteAllCookies();
            WebSocketServer.screenInfoMsg("第一次访问 %s", loginUrl);
            driver.get(loginUrl);
            WebSocketServer.screenInfoMsg("设置cookie lsession:%s", lsession);
            Cookie cookie1 = new Cookie("LSESSION", lsession);
            driver.manage().addCookie(cookie1);
            WebSocketServer.screenInfoMsg("第二次访问 %s", loginUrl);
            driver.get(loginUrl);
            By xpath = By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div[3]/div[1]/div[1]/div/a/div/div/div/div[2]/div[1]");
            my_profile = wait.until(ExpectedConditions.textToBe(xpath, "My Profile"));
            watch.stop();
        } catch (Exception e) {
            WebSocketServer.screenErrorMsg("异常: %s ", e.getMessage());
            e.printStackTrace();
        }
        WebSocketServer.screenInfoMsg("耗时: %s  个人信息菜单: %s", watch.getTotalTimeSeconds(), my_profile);

    }

}
